<?php defined('IN_LK') or die('Access Denied');
/**
 * 网站前台会员 
 * 有些方法冗余了你看的出来么
 * @access public
 * @author yangzhiguo 2011-05-14
 */
lk::lo_app('index_common', 'index', 0);
class index extends index_common
{
	private $member_db = null;
	private $group_db  = null;
	
	public function __construct()
	{
		parent::__construct();
		$this->member_db = lk::load_model('member');
		$this->group_db = lk::load_model('member_group');
	}
	
	/**
	 * 用户注册页面
	 * @access public 
	 */
	public function register()
	{
		if(isset($_POST['dosubmit']))
		{
			if(!isset($_POST['protocol']) || $_POST['protocol'] != 1)
			{
				showmsg(LANG('please_read_protocol'), false);
			}
			$info   = $_POST['info'];
			$fields = array('username', 'password1', 'password2', 'email', 'nickname', 'captcha');
			$info   = in_fields($info, $fields);
			if(strtolower($info['captcha']) != strtolower($_SESSION['captcha']))
			{
				showmsg(LANG('captcha_is_wrong'), false);
			}
			unset($info['captcha']);
			$r = $this->member_db->doregister($info);
			if($r)
			{
				$this->member_db->dologin($info['username'], password($info['password1']));
				header('Location:?m=member&a=register_success');
			}
			else
			{
				showmsg(LANG('register_faild'), false);
			}
		}
		else
		{
			include template('register');
		}
	}
	
	/**
	 * 注册成功
	 */
	public function register_success()
	{
		include template('register_success');
	}
	
	/**
	 * 登录
	 */
	public function login()
	{
		if(isset($_POST['dosubmit']))
		{
			if(strtolower($_POST['captcha']) != strtolower($_SESSION['captcha']))
			{
				showmsg(LANG('captcha_is_wrong'), false);
			}
			$password = $this->member_db->login_check($_POST['username'], $_POST['password']);
			$cookietime = isset($_POST['cookietime']) && $_POST['cookietime'] ? 2592000 : 0;
			$this->member_db->dologin($_POST['username'], $password, $cookietime);
			$back = isset($_POST['http_referer']) ? $_POST['http_referer'] : SITE_URL;
			showmsg(LANG('login_success'), true, $back);
		}
		else
		{
			//检测登录状态
			include template('login');
		}
	}
	
	/**
	 * 退出
	 */
	public function logout()
	{
		$this->dologout();
		showmsg(LANG('logout_success'), true, SITE_URL);
	}
	
	/**
	 * 注册协议
	 */
	public function protocol()
	{
		include template('reg_protocol');
	}
	
	/**
	 * 检查用户名是否存在
	 */
	public function check_username_ajax()
	{
		$username = isset($_GET['username']) ? $_GET['username'] : exit('0');
		if(!is_username($username) || $this->is_exists('username', $username))
		{
			exit('0');
		}
		exit('1');
	}
	
	/**
	 * 检查邮箱是否存在
	 */
	public function check_email_ajax()
	{
		$email  = isset($_GET['email']) ? $_GET['email'] : exit('0');
		if(!is_email($email) || $this->is_exists('email', $email))
		{
			exit('0');
		}
		exit('1');
	}	
	
	/**
	 * 检查昵称是否存在
	 */
	public function check_nickname_ajax()
	{
		$nickname = isset($_GET['nickname']) ? $_GET['nickname'] : exit('0');
		if(!is_username($nickname) || $this->is_exists('nickname', $nickname))
		{
			exit('0');
		}
		exit('1');
	}

	/**
	 * 检查某个字段是否存在重复
	 * @param  string $field   字段名
	 * @param  string $value   不允许重复的值
	 * @param  int    $allowid 允许重复的id（自己）
	 * @return bool   $r       存在返回true
	 */
	private function is_exists($field, $value, $allowid = 0)
	{
		$where  = "`$field`='$value'";
		$where .= $allowid ? " AND `userid`!='$allowid'" : '';
		return (boolean)$this->member_db->get_one('userid', $where);
	}
	
	
	/**
	 * 登出动作
	 */
	private function dologout()
	{
		$_SESSION['_userid'] = $_SESSION['_groupid'] = $_SESSION['_username'] = '';
		set_cookie('_lkauth', '');
	}
}